<template>
  <div class="goodlist">
    <!-- 搜索框 -->
    <div style="margin-top: 15px;">
      <el-input placeholder="请输入内容" class="input-with-select" style=" width:300px" v-model="searchKey">
        <el-button slot="append" icon="el-icon-search" type="success" plain></el-button>
      </el-input>
      <el-button type="success" plain @click='$router.push({path:"add"})'>添加商品</el-button>
    </div>
    <!-- 表格 -->
    <el-table :data="goodslist" height="400" border style="width: 100%;margin-top:30px;background-color='red' ">
      <el-table-column type="index" width="50">
      </el-table-column>
      <el-table-column prop="goods_name" label="商品名称">
      </el-table-column>
      <el-table-column prop="goods_price" label="商品价格" width="60">
      </el-table-column>
      <el-table-column prop="goods_weight" label="商品重量" width="80">
      </el-table-column>
      <el-table-column prop="add_time" label="创建时间" width="80">
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-tooltip content="编辑" placement="top">
            <el-button type="info" size="mini" icon="el-icon-edit" plain @click="showGoodsById(scope.row)"></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button type="danger" size="mini" icon="el-icon-delete" plain @click='deleteUser(scope.row.id)'></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="block">
      <el-pagination style="background-color:#545c64" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <!-- 编辑商品对话框 -->
    <el-dialog title="编辑商品" :visible.sync="editGoodsFormVisible">
      <el-form :model="addUserForm" label-width="100px" ref='addform' status-icon>
        <el-form-item label="商品名称">
          <el-input v-model="addUserForm.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="价格">
          <el-input v-model="addUserForm.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="数量">
          <el-input v-model="addUserForm.goods_number"></el-input>
        </el-form-item>
        <el-form-item label="重量">
          <el-input v-model="addUserForm.goods_weight"></el-input>
        </el-form-item>
        <el-form-item label="图片">
          <img :src="imgsrc" alt="">
          <el-upload class="upload-demo" action="http://localhost:8888/api/private/v1/upload" :before-upload='beforeUpload' :on-success='uploadSuccess' :headers="getToken()" :on-preview="handlePreview" :on-remove="handleRemove" :file-list="fileList" list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('addform')">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { goodsList, getGoodsById, editGoods } from '@/api/index.js'
export default {
  data () {
    return {
      // 图片预览路径
      imgsrc: '',
      // 图片列表
      fileList: [],
      addUserForm: {
        id: '',
        goods_name: '',
        goods_price: 0,
        goods_number: 0,
        goods_weight: 0,
        pics: [
          {
            pics_big: ''
          }
        ]
      },
      editGoodsFormVisible: false,
      goodslist: [],
      searchKey: '',
      pagesize: 10,
      currentPage: 1,
      total: 0
    }
  },
  methods: {
    // 图片上传
    // 从列表中移出图片
    handleRemove (file, fileList) {
      // 1、这里有个坑,无论图片上传之前有没有阻止，都会执行这块代码，因为阻止上传原理是，从列表中移除
      // 2、当你上传的第一张图片是符合判断的，那么会正常执行，当第二张图片不符合判断，那么就会报file.response.data找不到的错误，所以这里需要再加一个判断
      if (!file.response) {
        return false
      }
      var delIndex = this.addform.pics.findIndex(value => {
        return value.pic === file.response.data.tmp_path
      })
      this.addform.pics.splice(delIndex, 1)
    },
    // 图片预览
    handlePreview (file) {
      this.imgVisible = true
      // 这里需要拼接上服务器地址，否则是基于本地运行地址
      this.imgsrc = 'http://localhost:8888/' + file.response.data.tmp_path
    },
    // 图片上传前的函数
    beforeUpload (file) {
      if (file.size > 10000) {
        this.$message({
          type: 'warning',
          message: '图片大小进行10000字节'
        })
        return false
      }
    },
    // 图片上传成功时的函数
    uploadSuccess (response, file, fileList) {
      this.imgsrc = 'http://localhost:8888/' + response.data.tmp_path
    },
    // 设置图片上传请求头
    getToken () {
      var token = localStorage.getItem('token')
      return { Authorization: token }
    },
    addUser () {
      editGoods(this.addUserForm).then(res => {
        console.log(res)
      })
    },
    // 显示编辑对话框
    showGoodsById (row) {
      this.editGoodsFormVisible = true
      console.log(row)
      getGoodsById(row.goods_id).then(res => {
        console.log(res)
        this.addUserForm = res.data
        this.imgsrc = 'http://localhost:8888' + res.data.pics[0].pics_big
        this.addUserForm.id = row.goods_id
      })
    },
    deleteUser () {},
    // 分页
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      this.pagesize = val
      this.init()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.currentPage = val
      this.init()
    },
    init () {
      goodsList({
        query: '',
        pagenum: this.currentPage,
        pagesize: this.pagesize
      }).then(res => {
        console.log(res)
        this.goodslist = res.data.goods
        this.total = res.data.total
      })
    }
  },
  mounted () {
    this.init()
  }
}
</script>

<style lang="scss" scoped >
</style>
